﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Reg_ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //ExtNameSearch();
            BgUrlSearch();
            //ImportCssSearch();

        }

        private static List<string> ImportCssSearch()
        {
            //处理css文件中导入其他css文件的情况，返回 css文件名称
            /*
             * 
                @import "font-awesome.min.css";
                @import url("print.css") print;
                @import "common.css" screen, projection;
                @import url('landscape.css') screen and (orientation:landscape);
                @import url('mobile.css') (max-width: 680px);
             * 
             */
            List<string> lstImportName = new List<string>();

            string html = File.ReadAllText(@"C:\Users\mj\Desktop\FZhan1\demo.126e.com\theme\desktop\css\desktop.css", Encoding.UTF8);

            string reg = @".*?@import\s+(url\()?['""](.+\.css)\)?['""].+";

            var mats = Regex.Matches(html, reg, RegexOptions.Compiled | RegexOptions.IgnoreCase);

            foreach (Match mat in mats)
            {
                string reqFileName = mat.Groups[2].Value;
                lstImportName.Add(reqFileName);
            }

            return lstImportName;
        }

        private static void BgUrlSearch()
        {
            string content = File.ReadAllText(@"C:\Users\mj\Desktop\FZhan\demo.126e.com\theme\layui\css\layui.css", Encoding.UTF8);

            string reg = @"url\((['""])?(.+?)(['""])?\)";

            var mats = Regex.Matches(content, reg, RegexOptions.Compiled | RegexOptions.IgnoreCase );

            foreach (Match mat in mats)
            {
                string reqFileName = mat.Groups[2].Value;

                //Uri re_Uri = this.Uri.CombineUrl(reqFileName);

                //string reqUrl = re_Uri.AbsoluteUri;

                //string res_Path = re_Uri.LocalAbsPath();

                ////修改href src 关联的路径
                //content = content.Replace(reqFileName, reqFileName.TrimStart('/'));
                //try
                //{
                //    wc.DownloadFile(reqUrl, res_Path);
                //}
                //catch (Exception ex)
                //{
                //    lstExp.Add(ex);
                //}
            }

        }

        private static void ExtNameSearch()
        {
            string html = File.ReadAllText(@"C:\Users\mj\Desktop\FZhan1\demo.126e.com\crm.html", Encoding.UTF8);

            List<string> lstExt = new List<string>()
            {
                "jsp",
                "asp"
            };

            foreach (var extName in lstExt)
            {
                string reg = string.Format(@"(.+['""])(.+\.{0}(\?.+?)?)(['""].+)", extName);

                var mats = Regex.Matches(html, reg, RegexOptions.Compiled | RegexOptions.IgnoreCase);

                foreach (Match mat in mats)
                {
                    string reqFileName = mat.Groups[2].Value;
                    Console.WriteLine(reqFileName);

                }

            }
        }
    }
}
